/*global pi, ArrayBuffer */

// 图像模块：
// 像素素具的格式总是ARGB
// 图片的像素坐标是是从上到下，从左到右
pi.mod.define('pi.image', function (pi) {
	"use strict";
	var indexNew, indexLoad, indexFree, indexGetInfo, indexGetPixels, indexSetPixels, indexEncodeNew, indexEncodeSize, indexEncodeWrite, indexEncodeFree, module = {},
		imageDLLPath = "pi_image.dll";

	// 图像数据的文件格式
	module.UNKNOWN_TYPE = 0; // 未知类型
	module.BMP_TYPE = 1;
	module.ICO_TYPE = 2;
	module.JPEG_TYPE = 3;
	module.JNG_TYPE = 4;
	module.KOALA_TYPE = 5;
	module.LBM_TYPE = 6;
	module.IFF_TYPE = 6;
	module.MNG_TYPE = 7;
	module.PBM_TYPE = 8;
	module.PBMRAW_TYPE = 9;
	module.PCD_TYPE = 10;
	module.PCX_TYPE = 11;
	module.PGM_TYPE = 12;
	module.PGMRAW_TYPE = 13;
	module.PNG_TYPE = 14;
	module.PPM_TYPE = 15;
	module.PPMRAW_TYPE = 16;
	module.RAS_TYPE = 17;
	module.TARGA_TYPE = 18;
	module.TIFF_TYPE = 19;
	module.WBMP_TYPE = 20;
	module.PSD_TYPE = 21;
	module.CUT_TYPE = 22;
	module.XBM_TYPE = 23;
	module.XPM_TYPE = 24;
	module.DDS_TYPE = 25;
	module.GIF_TYPE = 26;
	module.HDR_TYPE = 27;
	module.FAXG3_TYPE = 28;
	module.SGI_TYPE = 29;
	module.EXR_TYPE = 30;
	module.J2K_TYPE = 31;
	module.JP2_TYPE = 32;
	module.PFM_TYPE = 33;
	module.WEBP_TYPE = 34;

	/**
	 * 创建空图像
	 * width: 整型，像素宽度
	 * height: 整型，像素高度
	 * 返回：整型，图像句柄，失败返回0
	 */
	indexNew = 0;
	module.create = function (width, height) {
		if (indexNew === 0) {
			indexNew = pi.vcall.register(imageDLLPath, "pi_img_new", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		return pi.vcall.invoke(indexNew, width, height);
	};

	/*
	 * 根据数据加载图像
	 * data: BufferArray，文件数据
	 * size：整型，文件数据字节数
	 * 返回：整型，图像句柄，失败返回0
	 */
	indexLoad = 0;
	module.load = function (data, size) {
		if (indexLoad === 0) {
			indexLoad = pi.vcall.register(imageDLLPath, "pi_img_load", pi.vcall.TYPE_INT, [pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT]);
		}
		return pi.vcall.invoke(indexLoad, data, size);
	};

	/**
	 * 释放图像句柄
	 * img：整型，图像句柄
	 * 返回：无返回值
	 */
	indexFree = 0;
	module.free = function (img) {
		if (indexFree === 0) {
			indexFree = pi.vcall.register(imageDLLPath, "pi_img_free", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT]);
		}
		pi.vcall.invoke(indexFree, img);
	};

	/**
	 * 取图像信息
	 * img：整型，图像句柄
	 * 返回：失败返回undefined，成功返回对象：{width: 宽度，height: 高度, fileType: 文件类型}
	 *
	 */
	indexGetInfo = 0;
	module.getInfo = function (img) {
		var r, desc, status, tempBuf;
		if (indexGetInfo === 0) {
			indexGetInfo = pi.vcall.register(imageDLLPath, "pi_img_get_info", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER]);
		}
		tempBuf = new ArrayBuffer(12);
		status = pi.vcall.invoke(indexGetInfo, img, tempBuf);
		r = undefined;
		if (status === 1) {
			desc = ["width", pi.vcall.TYPE_INT, "height", pi.vcall.TYPE_INT, "fileType", pi.vcall.TYPE_INT];
			r = pi.vcall.createObjectFromBuffer(tempBuf, desc);
		}
		return r;
	};

	/**
	 * 取图像指定位置(x, y)，指定宽高(w, h)的像素素组
	 * img：整型，图像句柄
	 * (x, y): 取像素数据的起始地址
	 * (w, h): 待取区域
	 * 返回：ArrayBuffer，失败返回undefined
	 */
	indexGetPixels = 0;
	module.getPixels = function (img, x, y, w, h) {
		var buf, size, status;
		if (indexGetPixels === 0) {
			indexGetPixels = pi.vcall.register(imageDLLPath, "pi_img_get_pixels", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		size = 4 * w * h;
		buf = new ArrayBuffer(size);
		status = pi.vcall.invoke(indexGetPixels, img, buf, size, x, y, w, h);
		if (status === 0) {
			buf = undefined;
		}
		return buf;
	};

	/**
	 * 设置图像指定位置(x, y)，指定宽高(w, h)的像素素组
	 * img：整型，图像句柄
	 * buf: ArrayBuffer，待设置的像素数据
	 * (x, y): 设置像素数据的起始地址
	 * (w, h): 待设置区域
	 * 返回：bool，失败返回false
	 */
	indexSetPixels = 0;
	module.setPixels = function (img, buf, x, y, w, h) {
		var status;
		if (indexSetPixels === 0) {
			indexSetPixels = pi.vcall.register(imageDLLPath, "pi_img_set_pixels", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		status = pi.vcall.invoke(indexSetPixels, img, buf, buf.byteLength, x, y, w, h);
		return (status === 1);
	};

	/**
	 * 图像编码：
	 * img：整型，图像句柄
	 * type: 整型，编码类型
	 * quality：整型，仅于值0到100之间，等于100，则为无损压缩
	 * 返回：ArrayBuffer，编码后的数据，失败返回0
	 */
	indexEncodeNew = 0;
	indexEncodeSize = 0;
	indexEncodeWrite = 0;
	indexEncodeFree = 0;
	module.encode = function (img, type, quality) {
		var buf, size, status, encodeData;
		if (indexEncodeNew === 0) {
			indexEncodeNew = pi.vcall.register(imageDLLPath, "pi_img_encode_new", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_INT, pi.vcall.TYPE_INT]);
		}
		if (indexEncodeSize === 0) {
			indexEncodeSize = pi.vcall.register(imageDLLPath, "pi_img_encode_size", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT]);
		}
		if (indexEncodeWrite === 0) {
			indexEncodeWrite = pi.vcall.register(imageDLLPath, "pi_img_encode_write", pi.vcall.TYPE_INT, [pi.vcall.TYPE_INT, pi.vcall.TYPE_POINTER, pi.vcall.TYPE_INT]);
		}
		if (indexEncodeFree === 0) {
			indexEncodeFree = pi.vcall.register(imageDLLPath, "pi_img_encode_free", pi.vcall.TYPE_VOID, [pi.vcall.TYPE_INT]);
		}

		buf = undefined;
		encodeData = pi.vcall.invoke(indexEncodeNew, img, type, quality);
		if (encodeData !== 0) {
			size = pi.vcall.invoke(indexEncodeSize, encodeData);
			if (size > 0) {
				buf = new ArrayBuffer(size);
				status = pi.vcall.invoke(indexEncodeWrite, encodeData, buf, size);
				if (status !== 1) {
					buf = undefined;
				}
			}
			pi.vcall.invoke(indexEncodeFree, encodeData);
		}
		return buf;
	};

	return module;
});